package MyList;

public class SingleLinkedList implements IList {

    static class ListNode {
        ListNode next;
        int val;
        public ListNode(int val) {
            this.val = val;
        }
    }
    ListNode head;

    //头插法
    public void addFirst(int data) {
        ListNode listNode = new ListNode(data);
        if(head == null) {
            head = listNode;
        }
        else {
            listNode.next = head;
            head = listNode;
        }
    }
    //尾插法
    public void addLast(int data) {
        ListNode listNode = new ListNode(data);
        if(head == null) {
            head = listNode;
        }
        else {
            ListNode cur = head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = listNode;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean IndextoAdd(int index) {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        //System.out.println(count);
        if(index >= 0 && index <= count) {
            return true;
        }
        return false;
    }
    public boolean addIndex(int index,int data) {
        if(IndextoAdd(index)) {
            ListNode listNode = new ListNode(data);
            if(index == 0) {
                addFirst(data);
                return true;
            }
            ListNode cur = head;
            index--;
            while(index > 0) {
                cur = cur.next;
                index--;
            }
            listNode.next = cur.next;
            cur.next = listNode;
        }
        return false;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key)
                return true;
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public ListNode Keycontains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.next.val == key)
                return cur;
            cur = cur.next;
        }
        return null;
    }
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = Keycontains(key);
        if(cur == null) {
            System.out.println("不存在" + key + "这个数");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = prev.next;
        while(cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = prev.next;
                cur = cur.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }
    }
    //得到单链表的长度
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            cur = cur.next;
            count ++;
        }
        return count;
    }

    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear() {
        head = null;
    }

}
